package com.bbh.plugin.single.privilege.core.service;


import com.bbh.plugin.single.privilege.core.enums.BaseEnum;
import com.bbh.plugin.single.privilege.core.repository.RoleMapper;
import com.bbh.plugin.single.privilege.core.vo.tree.RoleVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.utils.mapper.JsonMapper;
import com.bbh.plugin.single.privilege.core.entity.Role;
import com.fasterxml.jackson.databind.JavaType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.bbh.common.domain.Page;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.bbh.common.base.service.BaseService;
/**
 * Created by roman_000 on 2015/7/15.
 */
@Service
@Transactional
public class RoleService extends BaseService<Role, String> {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceService roleResourceService;


    /**
     * 查询用户角色列表
     *
     * @return
     */
    public Page<RoleVo> findUserRolePage(Map<String, Object> params, Page pageable) {
        PageHelper.startPage(pageable.getPageNum() + 1, pageable.getPageSize());
        List<RoleVo> roleVoList = roleMapper.findUserRoleList(params);
        PageInfo<RoleVo> pageInfo = new PageInfo<RoleVo>(roleVoList);
        return pageConverter(pageInfo);
    }


    public List<Role> findRoleListByLoginName(String appKey, String loginName) {
        Condition condition = new Condition();
        condition.put("loginName", loginName);
        condition.put("appKey", appKey);
        condition.put("isEnabled", BaseEnum.IS_ENABLED.ENABLED.getKey());
        condition.put("dataStatus", BaseEnum.DATA_STATUS.NORMAL.getKey());
        return roleMapper.findRoleListByLoginName(condition);
    }

    /**
     * 更新角色信息并更新资源信息
     */
    public int updateRoleSource(Role role, String checks, String loginName) {
        Assert.notNull(role.getId(), "参数错误");
        Role updateRole = super.selectByPrimaryKey(role.getId());
        if (updateRole != null) {
            Condition conditionNew = new Condition();
            if (StringUtils.isNotBlank(role.getEnname())) {
                conditionNew.putEquals("enname", role.getEnname());
            }
            Condition entityCondition = new Condition();
            entityCondition.putEquals("enname", updateRole.getEnname());
            if (!super.existsUpdateCheckEach(conditionNew, entityCondition)) {
                Assert.isTrue(!"0".equals(updateRole.getIsSys()), "内置角色不允许编辑");
                updateRole.setName(role.getName());
                updateRole.setEnname(role.getEnname());
                updateRole.setIsEnabled(role.getIsEnabled());
                updateRole.setDescription(role.getDescription());
                updateRole.setModifyBy(loginName);
                updateRole.setModifyTime(new Date());
                //更新资源权限
                List<HashMap<String, Object>> checkParams = Lists.newArrayList();
                if (StringUtils.isNotBlank(checks)) {
                    JsonMapper mapper = JsonMapper.nonEmptyMapper();
                    JavaType beanListType = mapper.contructCollectionType(List.class, HashMap.class);
                    String result = StringEscapeUtils.unescapeHtml4(checks);
                    checkParams = mapper.fromJson(result, beanListType);
                    roleResourceService.del(role.getId());//删除之前的资源权限
                    roleResourceService.insert(role.getId(), checkParams);
                }
                super.updateByPrimaryKey(updateRole);
                return 1;
            }
            return 2;
        }
        return 0;
    }
}